package com.heying.springcore.aop.jdbc.repository.jdbc;

import com.heying.springcore.aop.jdbc.entity.Customer;
import com.heying.springcore.aop.jdbc.repository.BookRepository;
import com.heying.springcore.aop.jdbc.repository.CustomerRepository;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.inject.Inject;
import java.sql.ResultSet;
import java.sql.SQLException;

@Repository
public class JdbcCustomerRepository implements CustomerRepository {

    private final JdbcTemplate jdbcTemplate;

    private BookRepository bookRepository;

    public JdbcCustomerRepository(JdbcTemplate jdbcTemplate, BookRepository bookRepository) {
        this.jdbcTemplate = jdbcTemplate;
        this.bookRepository = bookRepository;
    }


    @Override
    public Customer queryCustomerByCustomer(String Customer) {
        return jdbcTemplate.queryForObject("select * from tbl_customer where customer=?",
                this::mapToCustomer,Customer);
    }

    private Customer mapToCustomer(ResultSet rs,int row) throws SQLException {
        System.out.println("row"+row);
        return new Customer(rs.getInt("customer"),
               rs.getString("customerName"),
                rs.getString("city"),
                rs.getString("state"),
                rs.getString("zip"),
                rs.getInt("referred"),
                rs.getString("telNo"),
                rs.getString("address"),
                bookRepository.queryBooksByCustomer(rs.getString("stuno")));
    }

}
